/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import javax.ejb.Stateful;
import javax.ejb.LocalBean;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;
import javax.persistence.EntityManager;
import entity.PaymentManagementEntity;
import entity.StakeholderEntity;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import net.sf.jasperreports.engine.util.JRLoader;
import org.primefaces.event.SelectEvent;
import session.stateful.StakeholderSessionBeanStateful;
import session.stateful.TransactionSessionBean;

/**
 *
 * @author Dao Thai Duy
 */
@Stateful
@LocalBean
public class ReportSessionBean {
    private EntityManager entityManager;
    @EJB
    private TransactionSessionBean transactionSessionBean;
    @EJB
    private StakeholderSessionBeanStateful stakeholderSessionBean;

    public void doCreateBill() throws ClassNotFoundException {
        PaymentManagementEntity currentBill = null;
        StakeholderEntity selectedStakeholder;
        selectedStakeholder=stakeholderSessionBean.getStakeholderById(Long.valueOf("1"));
        currentBill = transactionSessionBean.getCurrentBill(selectedStakeholder);
        System.out.println("currentBill " + currentBill.getId());
        createBill(selectedStakeholder, currentBill);
    }

    public void createBill(StakeholderEntity selectedStakeholder, PaymentManagementEntity currentBill) throws ClassNotFoundException {
        HashMap parameterMap = new HashMap();
        JasperReport jasperReport;
        Connection connection = establishConnection();

        try {
            jasperReport = JasperCompileManager.compileReport("C://IS3102Project//report//reportBill.jrxml");
//            jasperReport= (JasperReport)JRLoader.loadObject(("reportBill.jasper"));
//            parameterMap.put("previousBilling", previousBill.getTotalPaymentCurrent());
//            parameterMap.put("previousPaymentLeft", previousBill.getPaymentLeft());
//            parameterMap.put("previousPaymentReceived", previousBill.getPaymentReceived());
            parameterMap.put("currentBillingId", currentBill.getId());
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, connection);

            JasperExportManager.exportReportToPdfFile(jasperPrint, "C://IS3102Project//IS3102Project-war//web//report//reportBill" + selectedStakeholder.getId() + currentBill.getId() + ".pdf");
//            JasperExportManager.exportReportToHtmlFile(jasperPrint, "D://NetBeansProjects//IS3102Project//IS3102Project-war//web//reportBill.html" );

//            JRXlsExporter exporter = new JRXlsExporter();
//            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            //export report to Excel
//            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "D://simple_report.xls" );
//            exporter.exportReport();
        } catch (JRException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static Connection establishConnection() throws ClassNotFoundException {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String myURL = "jdbc:mysql://localhost:3306/is3102db";
            connection = DriverManager.getConnection(myURL, "root", "adminadmin");
            connection.setAutoCommit(false);
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
        return connection;
    }
}
